package com.winai.launcher.override;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;

import com.winai.launcher.base.db.DB_Common;
import com.winai.launcher.constant.Constant;
import com.winai.launcher.constant.ScreenProtectConstant;
import com.winai.launcher.manager.ScreenProtectManager;
import com.winai.launcher.utils.BaseUtils;
import com.winai.launcher.view.ScreenProtectActivity;

/**
 * 作者：康少
 * 时间：2019/11/28 0028
 * 说明：倒计时类
 */
public class CountTimer extends CountDownTimer {
    private Context context;
    @SuppressLint("StaticFieldLeak")
    private static CountTimer mInstance;

    public static CountTimer getInstance(Context context, int delayedTime) {
        if (mInstance == null) {
            synchronized (CountTimer.class) {
                if (mInstance == null && ScreenProtectConstant.time != ScreenProtectConstant.NEVER) {
                    mInstance = new CountTimer(ScreenProtectConstant.time - delayedTime, 1000, context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 说明：重置计时器
     */
    public static void restart(Context context) {
        restart(context, 0);
    }

    /**
     * 说明：重置计时器,并添加延迟
     */
    public static void restart(Context context, int delayedTime) {
        if (mInstance != null) {
            mInstance.cancel();
            mInstance = null;
        }
        CountTimer mInstance = getInstance(context.getApplicationContext(), delayedTime);
        if (mInstance != null) {
            if (ScreenProtectConstant.time != ScreenProtectConstant.NEVER
                    && !BaseUtils.isAPPBroughtToBackground(context, "com.qiyi.video.speaker"
                    , "com.qiyi.video.speaker.activity.PlayerActivity")
                    && !Constant.isPlayVideo)
                mInstance.start();
        }
    }

    /**
     * 说明：释放定时器
     */
    public static void release() {
        if (mInstance != null) {
            mInstance.cancel();
        }
        mInstance = null;
    }

    /**
     * 参数 millisInFuture       倒计时总时间（如60S，120s等）
     * 参数 countDownInterval    渐变时间（每次倒计1s）
     */
    public CountTimer(long millisInFuture, long countDownInterval, Context context) {
        super(millisInFuture, countDownInterval);
        this.context = context;
    }

    // 计时完毕时触发
    @Override
    public void onFinish() {
        if (!BaseUtils.isAPPBroughtToBackground(context, "com.qiyi.video.speaker"
                , "com.qiyi.video.speaker.activity.PlayerActivity") && !Constant.isPlayVideo) {
            //如果没有播放爱奇艺和艾益生视频，则打开屏保
            DB_Common dbCommon = new DB_Common(context);
            String isFirst = dbCommon.get("isFirstOpenApp");

            if (!TextUtils.isEmpty(isFirst) && ScreenProtectConstant.time != ScreenProtectConstant.NEVER
                    && ScreenProtectManager.getModel() != ScreenProtectManager.ScreenProtectModel.NULL) {
                //如果屏保处于开启状态，则不重新进入屏保页面
                Log.d("进入屏保倒计时结束：", "进入屏保");

                Intent intent = new Intent(context, ScreenProtectActivity.class);
                intent.putExtra("fromSetCenter", "false");
                context.startActivity(intent);
            } else if (!TextUtils.isEmpty(isFirst) && ScreenProtectManager.getModel() == ScreenProtectManager.ScreenProtectModel.NULL) {
                //如果设置的无屏保，并且处于引导完成状态，则跳转到广告轮播图
                Log.d("进入屏保倒计时结束：", "进入轮播图");

                //模拟HOME
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                context.startActivity(intent);
            } else {
                Log.d("进入屏保倒计时结束：", "未设置屏保 或 引导页未完成");
            }
        } else {
            Log.d("进入屏保倒计时结束：", "正在播放爱奇艺视频 或 艾益生视频，不进入屏保");
        }
    }

    // 计时过程显示
    @Override
    public void onTick(long millisUntilFinished) {
        Log.d("进入屏保倒计时：", millisUntilFinished + "。计时器：" + mInstance);
    }
}
